package com.dragon.sdgp.util.defaultGenerate.strategy;

import com.dragon.sdgp.util.CommonUtil;
import com.dragon.sdgp.util.defaultGenerate.DefaultDataSources;

/**
 * @AUTHOR : Dragon_Yang
 * @File : PersonNameGenerateStrategy
 * @DESCRIPTION :
 * @DATE : 2023/3/17 17:50
 */
public class PersonNameGenerateStrategy implements GenerateStrategy{
    @Override
    public String createOne() {
        return createPersonName();
    }

    /**
     * 创建一个人名
     * @return
     */
    private String createPersonName(){
        //姓名总长度，20%概率2个字，60%概率3个字，20%概率4个字
        int[] nums = new int[]{2, 3, 3, 3, 4, 3, 2, 4, 3, 3};
        int maxLen = 30;

        //80%概率取前30个姓, 20%概率在所有的姓氏中取
        if(CommonUtil.getRandom(10) < 8){
            maxLen = DefaultDataSources.FIRST.length;
        }

        String first = CommonUtil.getOneRandomInArray(DefaultDataSources.FIRST, maxLen);
        int len = nums[CommonUtil.getRandom(nums.length)];

        //如果名字长度是2，姓氏也是2，就增加名字长度
        if(len == 2 && first.length() == 2) len += (1 + (int) (Math.random() * 2));

        String last = CommonUtil.getOneRandomInArray(DefaultDataSources.LAST);

        if((int) (Math.random() * 2) == 0){
            if(len - first.length() == 1){
                int r = CommonUtil.getRandom(0, 2);
                last = last.substring(r, r + 1);
            }
        }else {
            int r = CommonUtil.getRandom(2);
            last = last.substring(r, r + 1);
            if(len - first.length() - last.length() == 1){
                first += last;
                r = CommonUtil.getRandom(2);
                last = CommonUtil.getOneRandomInArray(DefaultDataSources.LAST);
                last = last.substring(r, r + 1);
            }
        }

        String name = first + last;
        return name;
    }
}
